import pymysql
from common.config import Config

config = Config()
host = config.get_mysql("host")
port = config.get_mysql("port")
user = config.get_mysql("user")
password = config.get_mysql("password")
db = config.get_mysql("database")
class Mysql:

    def __enter__(self):
        self.conn = pymysql.connect(host=host, port=int(port), db=db, user=user,
                                    password=password)
        self.cursor = self.conn.cursor()
        return self

    def __exit__(self, *exc_info):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

    def fetchall(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def fetchone(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()


if __name__ == "__main__":
    with Mysql() as mysql:
        print(mysql.fetchall("select * from jia1"))
